package cn.smile.controller.admin;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.smile.bean.CodeMsg;
import cn.smile.bean.PageBean;
import cn.smile.bean.Result;
import cn.smile.entity.common.Goods;
import cn.smile.entity.common.GoodsCategory;
import cn.smile.entity.common.Student;
import cn.smile.service.common.GoodsCategoryService;
import cn.smile.service.common.GoodsService;
import cn.smile.service.common.StudentService;

import javax.annotation.Resource;

/**
 * 后台物品管理控制器
 */
@RequestMapping("/admin/goods")
@Controller
public class GoodsController {

    @Resource
    private GoodsCategoryService goodsCategoryService;
    @Resource
    private GoodsService goodsService;
    @Resource
    private StudentService studentService;

    /**
     * 物品管理列表页面
     */
    @RequestMapping(value = "/list")
    public String list(Goods goods, PageBean<Goods> pageBean, Model model) {
        if (goods.getStudent() != null && goods.getStudent().getSn() != null) {
            Student student = studentService.findBySn(goods.getStudent().getSn());
            if (student != null) {
                goods.setStudent(student);
            }
        }
        if (goods.getGoodsCategory() != null && goods.getGoodsCategory().getName() != null) {
            List<GoodsCategory> goodsCategory = goodsCategoryService.findByName(goods.getGoodsCategory().getName());
            if (goodsCategory != null && !goodsCategory.isEmpty()) {
                goods.setGoodsCategory(goodsCategory.get(0));
            }
        }
        goods.setStatus(-1);
        model.addAttribute("title", "物品列表");
        model.addAttribute("name", goods.getName());
        model.addAttribute("goodsCategoryName", goods.getGoodsCategory() == null ? null : goods.getGoodsCategory().getName());
        model.addAttribute("sn", goods.getStudent() == null ? null : goods.getStudent().getSn());
        model.addAttribute("pageBean", goodsService.findlist(pageBean, goods));
        return "admin/goods/list";
    }


    /**
     * 物品上架
     */
    @RequestMapping(value = "/up_down", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> upDown(@RequestParam(name = "id") Long id, @RequestParam(name = "status") Integer status) {
        Goods goods = goodsService.findById(id);
        if (goods == null) {
            return Result.error(CodeMsg.ADMIN_GOODS_NO_EXIST);
        }
        if (goods.getStatus() == status) {
            return Result.error(CodeMsg.ADMIN_GOODS_STATUS_NO_CHANGE);
        }
        if (status != Goods.GOODS_STATUS_UP && status != Goods.GOODS_STATUS_DOWN) {
            return Result.error(CodeMsg.ADMIN_GOODS_STATUS_ERROR);
        }
        if (goods.getStatus() == Goods.GOODS_STATUS_SOLD) {
            return Result.error(CodeMsg.ADMIN_GOODS_STATUS_UNABLE);
        }
        goods.setStatus(status);
        //进行更新数据库
        if (goodsService.save(goods) == null) {
            return Result.error(CodeMsg.ADMIN_GOODS_EDIT_ERROR);
        }
        return Result.success(true);
    }

    /**
     * 物品推荐或取消推荐
     */
    @RequestMapping(value = "/recommend", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> recommend(@RequestParam(name = "id") Long id, @RequestParam(name = "recommend") Integer recommend) {
        Goods goods = goodsService.findById(id);
        if (goods == null) {
            return Result.error(CodeMsg.ADMIN_GOODS_NO_EXIST);
        }
        if (goods.getRecommend() == recommend) {
            return Result.error(CodeMsg.ADMIN_GOODS_STATUS_NO_CHANGE);
        }
        if (recommend != Goods.GOODS_RECOMMEND_OFF && recommend != Goods.GOODS_RECOMMEND_ON) {
            return Result.error(CodeMsg.ADMIN_GOODS_STATUS_ERROR);
        }
        if (goods.getStatus() == Goods.GOODS_STATUS_SOLD) {
            return Result.error(CodeMsg.ADMIN_GOODS_STATUS_UNABLE);
        }
        goods.setRecommend(recommend);
        //进行更新数据库
        if (goodsService.save(goods) == null) {
            return Result.error(CodeMsg.ADMIN_GOODS_EDIT_ERROR);
        }
        return Result.success(true);
    }


    /**
     * 物品删除操作
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> delete(@RequestParam(name = "id") Long id) {
        try {
            goodsService.delete(id);
        } catch (Exception e) {
            return Result.error(CodeMsg.ADMIN_GOODS_DELETE_ERROR);
        }
        return Result.success(true);
    }
}
